package com.rupeng.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Permission;
import com.rupeng.pojo.Role;
import com.rupeng.service.PermissionService;
import com.rupeng.service.RoleService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService = new RoleService();
	@Autowired
	private PermissionService permissionService = new PermissionService();
	@RequestMapping("/list.do")
	public ModelAndView list() {
		List<Role> roles = roleService.selectList();
		ModelAndView modelAndView = new ModelAndView("role/list");
		modelAndView.addObject("roles", roles);
		return modelAndView;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage() {
		List<Permission> permissions = permissionService.selectList();
		ModelAndView modelAndView = new ModelAndView("role/add");
		modelAndView.addObject("permissions", permissions);
		return modelAndView;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(String name, String description, Long[] permissionIds) {
		if (CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description) || permissionIds==null || permissionIds.length<1) {
			return AjaxResult.errorInstance("表单数据不能为空");
		}
		Role role = new Role();
		role.setName(name);
		roleService.selectList(role);
		if (roleService.isExisted(role)) {
			return AjaxResult.errorInstance("该权限名已存在");
		}
		role.setDescription(description);
		roleService.insert(role, permissionIds);
		return AjaxResult.successInstance("角色添加成功");
	}
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public ModelAndView updatePage(Long id) {
		Role role = roleService.selectOne(id);
		return new ModelAndView("role/update","role",role);
	}
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updateSubmit(Long id, String name, String description) {
		if (CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description)) {
			return AjaxResult.errorInstance("表格字段为空");
		}
		Role role = new Role();
		role.setName(name);
		role = roleService.selectOne(role);
		if (role!=null && role.getId()!=id) {
			return AjaxResult.errorInstance("该权限名已存在");
		}
		role = new Role();
		role.setId(id);
		role.setName(name);
		role.setDescription(description);
		roleService.update(role);
		return AjaxResult.successInstance("修改权限成功");
	}
	@RequestMapping(value="/delete.do")
	public @ResponseBody AjaxResult delete(Long id) {
		roleService.delete(id);
		return AjaxResult.successInstance("权限删除成功");
	}
}